package com.timothy.android;


import java.util.ArrayList;
import java.util.List;

import com.timothy.question.Choice;
import com.timothy.question.STTwoButtonAlpha;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LayoutB extends BaseQuestionActivity{
	
	private static final String LOG_TAG = "LayoutB";
	
	private LinearLayout layoutB;
	private TextView tv_msg = null;
	private Button btnA = null;
	private Button btnB = null;
	private Button btnC = null;
	
	private Button btnPre = null;
	private Button btnNext = null;
	private Drawable btnDrawable;
	
	STTwoButtonAlpha sTTwoButtonAlpha = null;
	
	private boolean countDownMark;
	private int countDownTime;
	private int countDownInterval;
	MyCount mc;
	
	public void initialSetting(){
		
		//layoutB
		layoutB = (LinearLayout) this.findViewById(R.id.layoutB);
		layoutB.setBackgroundColor(LayoutBConfig.LAYOUTB_BG_COLOR);
		layoutB.setPadding(LayoutBConfig.LAYOUTB_PADDING, LayoutBConfig.LAYOUTB_PADDING, 
				LayoutBConfig.LAYOUTB_PADDING, LayoutBConfig.LAYOUTB_PADDING);
		
		//tv_msg 
		tv_msg = (TextView) this.findViewById(R.id.promotMsg);
		tv_msg.setHeight(LayoutBConfig.TV_MSG_HEIGHT);
		tv_msg.setTextSize(LayoutBConfig.TV_MSG_TEXTSIZE);
		tv_msg.setBackgroundColor(LayoutBConfig.TV_MSG_BG_COLOR);
		tv_msg.setTextColor(LayoutBConfig.TV_MSG_TEXTCOLOR);
		
		btnDrawable = this.getResources().getDrawable(R.drawable.button);
		
		//设置ButtonA
		btnA = (Button) this.findViewById(R.id.btnA);
		btnA.setHeight(LayoutBConfig.BUTTONA_HEIGHT);
		btnA.setTextColor(LayoutBConfig.BUTTONA_TEXTCOLOR);
		btnA.setTextSize(LayoutBConfig.BUTTONA_TEXTSIZE);
		btnA.setBackgroundDrawable(btnDrawable); 
		
		//设置ButtonB
		btnB = (Button) this.findViewById(R.id.btnB);
		btnB.setHeight(LayoutBConfig.BUTTONB_HEIGHT);
		btnB.setTextColor(LayoutBConfig.BUTTONB_TEXTCOLOR);
		btnB.setTextSize(LayoutBConfig.BUTTONB_TEXTSIZE);
		btnB.setBackgroundDrawable(btnDrawable); 
		
		//设置ButtonC
		btnC = (Button) this.findViewById(R.id.btnC);
		btnC.setHeight(LayoutBConfig.BUTTONC_HEIGHT);
		btnC.setTextColor(LayoutBConfig.BUTTONC_TEXTCOLOR);
		btnC.setTextSize(LayoutBConfig.BUTTONC_TEXTSIZE);
		btnC.setBackgroundDrawable(btnDrawable); 
		
		//countDown
		countDownMark = LayoutBConfig.COUNT_DOWN_MARK;
		countDownTime = LayoutBConfig.COUNT_DOWN_TIME; 
		countDownInterval = LayoutBConfig.COUNT_DOWN_INTERVAL;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layoutb);
		
		initialSetting();
		
		//mock information
		Bundle bundle = this.getIntent().getExtras();
		String question = bundle.getString("question");
		String choice1 = bundle.getString("choice1");
		String choice2 = bundle.getString("choice2");
		String choice3 = bundle.getString("choice3");
		
		sTTwoButtonAlpha = new STTwoButtonAlpha();
		List<Choice> mockChoices = new ArrayList<Choice>();
		sTTwoButtonAlpha.setQuestionName(question);
		mockChoices.add(new Choice("Y",choice1));
		mockChoices.add(new Choice("A",choice2));
		mockChoices.add(new Choice("N",choice3));
		sTTwoButtonAlpha.setChoices(mockChoices);
		

		tv_msg.setText(sTTwoButtonAlpha.getQuestionName());
		List<Choice> choices = sTTwoButtonAlpha.getChoices();
		btnA.setText(choices.get(0).getContent());
		btnB.setText(choices.get(1).getContent());
		btnC.setText(choices.get(2).getContent());
        
		btnA.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				btnA.setBackgroundColor(Color.YELLOW);
				btnB.setBackgroundDrawable(btnDrawable); 
				btnC.setBackgroundDrawable(btnDrawable); 
			}
		});
		
		btnB.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				btnA.setBackgroundDrawable(btnDrawable);
				btnB.setBackgroundColor(Color.YELLOW); 
				btnC.setBackgroundDrawable(btnDrawable); 
			}
		});
		
		btnC.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				btnA.setBackgroundDrawable(btnDrawable);
				btnB.setBackgroundDrawable(btnDrawable); 
				btnC.setBackgroundColor(Color.YELLOW); 
			}
		});

		//navigation buttons
		btnPre = (Button) this.findViewById(R.id.btnPre);
		btnPre.setEnabled(false);
		btnPre.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
			    intent.setClass(LayoutB.this,LayoutA.class);
			    startActivity(intent);
			}
		});
		
		btnNext = (Button) this.findViewById(R.id.btnNext);
		btnNext.setEnabled(false);
		btnNext.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
			    intent.setClass(LayoutB.this,LayoutA.class);
			    startActivity(intent);
			}
		});
		
		if(countDownMark){
			List<Button> btnList = new ArrayList<Button>(); 
			btnList.add(btnA);
			btnList.add(btnB);
			btnList.add(btnC);
	        mc = new MyCount(countDownTime * 1000,countDownInterval * 1000,btnList,this.getBaseContext());  
	        mc.start();			
		}
	}
	
	//Activity 销毁时的操作
	protected void onDestroy() {
        super.onDestroy();
        
        //恢复手机原来锁屏时间
//        Settings.System.putInt(this.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, lockScreenTime);
        
     
    }
	
	//倒计时内部类
	/*class MyCount extends CountDownTimer { 
		List<Button> btnList = new ArrayList<Button>();
		
		public MyCount(long millisInFuture, long countDownInterval,List<Button> btnList) {   
			super(millisInFuture, countDownInterval);
			this.btnList = btnList;
		}   
		
		@Override   
		public void onFinish() {   
			for(int i=0;i<btnList.size();i++){
				Button btn = btnList.get(i);
				btn.setEnabled(false);
				btn.setBackgroundColor(Color.GRAY);
			}
		}   
		
		@Override   
		public void onTick(long millisUntilFinished) {
			Toast.makeText(LayoutB.this, millisUntilFinished / 1000 + "", Toast.LENGTH_SHORT).show();//toast有显示时间延迟T
		} 
		
		public void showToast(){
	    	LayoutInflater li=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    	View view=li.inflate(R.layout.countdown_toast,null);
	    	//把布局文件toast.xml转换成一个view
	    	Toast toast= new Toast();
	    	toast.setView(view);
	    	//载入view,即显示toast.xml的内容
	    	TextView tv=(TextView)view.findViewById(R.id.countDownTV);
	    	tv.setText("Toast View");
	    	//修改TextView里的内容
	    	toast.setDuration(Toast.LENGTH_SHORT);
	    	//设置显示时间，长时间Toast.LENGTH_LONG，短时间为Toast.LENGTH_SHORT,不可以自己编辑
	    	toast.show();
	    }
	}*/
	

}
